package com.hjm.project.system.sysConfigJob.controller;

import com.hjm.common.constant.UserConstants;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.system.job.domain.SysJob;
import com.hjm.project.system.job.service.ISysJobService;
import com.hjm.project.system.sysConfigJob.domain.SysConfigJob;
import com.hjm.project.system.sysConfigJob.service.ISysConfigJobService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 参数配置 信息操作处理
 */
@Controller
@RequestMapping("/system/sysConfigJob")
public class SysConfigJobController extends BaseController
{
    private String prefix = "system/sysConfigJob";

    @Resource
    private ISysConfigJobService iSysConfigJobService;

    @Resource
    private ISysJobService iSysJobService;

    @RequiresPermissions("system:config:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/config";
    }

    /**
     * 查询参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysConfigJob config)
    {
        startPage();
        List<SysConfigJob> list = iSysConfigJobService.selectConfigList(config);
        return getDataTable(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysConfigJob config)
    {
        List<SysConfigJob> list = iSysConfigJobService.selectConfigList(config);
        ExcelUtil<SysConfigJob> util = new ExcelUtil<SysConfigJob>(SysConfigJob.class);
        return util.exportExcel(list, "参数数据");
    }

    /**
     * 新增参数配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存参数配置
     */
    @RequiresPermissions("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysConfigJob config)
    {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(iSysConfigJobService.checkConfigKeyUnique(config)))
        {
            return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(iSysConfigJobService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @GetMapping("/edit/{configId}")
    public String edit(@PathVariable("configId") Long configId, ModelMap mmap)
    {
        mmap.put("config", iSysConfigJobService.selectConfigById(configId));
        return prefix + "/edit";
    }

    /**
     * 修改保存参数配置
     */
    @RequiresPermissions("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysConfigJob config)
    {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(iSysConfigJobService.checkConfigKeyUnique(config)))
        {
            return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        iSysConfigJobService.updateConfig(config);
        return success();
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(iSysConfigJobService.deleteConfigByIds(ids));
    }

    /**
     * 校验参数键名
     */
    @PostMapping("/checkConfigKeyUnique")
    @ResponseBody
    public String checkConfigKeyUnique(SysConfigJob config)
    {
        return iSysConfigJobService.checkConfigKeyUnique(config);
    }


    /**
     * 任务调度立即执行一次
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @RequiresPermissions("monitor:job:changeStatus")
    @PostMapping("/run")
    @ResponseBody
    public AjaxResult run(SysConfigJob sysConfigJob) throws SchedulerException {
        List<SysConfigJob> sysConfigJobs = iSysConfigJobService.selectConfigList(sysConfigJob);
        if (sysConfigJobs.get(0).getConfigType().equals("Y")) {
            throw new BusinessException("当前任务已执行,不可重复执行");
        }

        if (sysConfigJobs.get(0).getConfigType().equals("Z")) {
            throw new BusinessException("当前任务正在执行中,请稍后执行");
        }

        SysJob sysJob = new SysJob();
        sysJob.setJobKey(sysConfigJob.getConfigKey());
        List<SysJob> sysJobList = iSysJobService.selectJobList(sysJob);
        if (CollectionUtils.isEmpty(sysJobList)) {
           throw new BusinessException("获取不到相对应的定时任务");
        }
        iSysJobService.run(sysJobList.get(0));
        return success();
    }

    /**
     * 前往定时任务页面
     */
    @GetMapping("/jobList")
    public String jobList(SysConfigJob sysConfigJob,ModelMap mmap) {
        mmap.put("jobKey", sysConfigJob.getConfigKey());
        return "system/job/job";
    }



}
